/**   
* @Title: LocalCacheContainer.java 
* @Package com.farms.cache 
* @Description: JVM本地缓存，用于存储以下信息：1、字典；2、配置参数；3、标准错误号；4、标准字段；
* 实现方式为：Param@ <110000,<110000,参数1,启用,....>>,<110001,<110001,参数1,启用,....>>
* 		  Error@ <110000,<110000,错误号1,启用,....>>,<110001,<110001,错误号1,启用,....>>
		  Dict@ <110000,<110000,字典1,启用,....>>,<110001,<110001,字典1,启用,....>>
		  DictSub#110000@ <110000-1,<110000-1,,启用,....>>,<110000-2,<110000-2,字典1,启用,....>>
		  DictSub#110001@ <110001-1,<110001-1,,启用,....>>,<110001-2,<110001-2,字典1,启用,....>>
		  Field@
* @author zjhua@hundsun.com   
* @date 2016年4月25日 下午9:53:03 
* @version V1.0   
*/ 
package com.medsoft.dlcache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Repository;

/**
 * @author zjhua
 *
 */
@Repository
public class LocalCacheContainer {
	private static final ConcurrentHashMap<String,ConcurrentHashMap<String,CacheEntry>> Caches = new ConcurrentHashMap<String,ConcurrentHashMap<String,CacheEntry>>();
	private static final ConcurrentHashMap<String,ConcurrentHashMap<String,CacheEntry>> CacheDictSubs = new ConcurrentHashMap<String,ConcurrentHashMap<String,CacheEntry>>();
	
	public CacheEntry getCacheEntry(String cacheName,String key) {
		if(Caches.containsKey(cacheName)) {
			if(cacheName.lastIndexOf("DictSub") >= 0) {
				return CacheDictSubs.get(key.split("__")[0]).get(key.split("__")[1]);
			}
			return Caches.get(cacheName).get(key);
		}
		return null;
	}
	
	public boolean saveCacheEntry(String cacheName,CacheEntry cacheEntry) {
		if(Caches.get(cacheName) == null) {
			return false;
		}
		if(cacheName.lastIndexOf("DictSub") >= 0) {
			CacheDictSubs.get(cacheEntry.getKey().split("__")[0]).put(cacheEntry.getKey().split("__")[1],cacheEntry);
			return true;
		}
		Caches.get(cacheName).put(cacheEntry.getKey(), cacheEntry);
		return true;
	}
	
	public boolean removeCacheEntry(String cacheName,String key) {
		if(Caches.get(cacheName) == null) {
			return false;
		}
		if(cacheName.lastIndexOf("DictSub") >= 0) {
			CacheDictSubs.get(key.split("__")[0]).remove(key.split("__")[1]);
			return true;
		}
		Caches.get(cacheName).remove(key);
		return true;
	}
	
	public Map<String,CacheEntry> getCache(String cacheName) {
		if(cacheName.lastIndexOf("DictSub") >= 0) {
			return CacheDictSubs.get(cacheName.split("__")[1]);
		}
		return Caches.get(cacheName);
	}
	
	public boolean replaceCache(String cacheName,ConcurrentHashMap<String,CacheEntry> cache) {
		if(cacheName.lastIndexOf("DictSub") >= 0) {
			for (Map.Entry<String, CacheEntry> entry : cache.entrySet()) {
				if (CacheDictSubs.containsKey(entry.getKey().split("__")[0])) {
					//NOP
				} else {
					CacheDictSubs.put(entry.getKey().split("__")[0], new ConcurrentHashMap<String,CacheEntry>());
				}
				CacheDictSubs.get(entry.getKey().split("__")[0]).put(entry.getKey().split("__")[1], entry.getValue());
			}
			return true;
		}
		Caches.put(cacheName,cache);
		return true;
	}
}
